<?php
	class Admin_Menu extends MY_Controller {
		function Admin_Menu() {
			parent::__construct();
			@session_start();
			$this->mod_ad->is_logged_in();		
			$_SESSION['redirect'] = $this->uri->uri_string();
			//kiem tra quyen truy cap 
			$this->check_permission();
		}
		
		function index(){
			$data['vl_sort'] = "DESC";	
			$data['title'] = "Quản lý danh mục menu - trang quản trị";
			$data['table'] = 'admin_menu';
			$data['controller'] = 'admin_menu';
			$data['list_menu'] = $this->mod_lib->findOrderList($data['table']);
			$data['content_admin'] = 'back_end/admin_menu/index';
			$data['sort'] = 'ASC';
			$this->db->order_by('parent_id','asc');
			
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			$per_page = 10;
			$uri = 4;
			
			if(isset($_SESSION['order_sort'])) {
				$data['admin_menu_value'] = $this->mod_lib->load_all('admin_menu','',array(),$per_page,$this->uri->segment($uri),$_SESSION['order_sort']);
			} else {
				$data['admin_menu_value'] = $this->mod_lib->load_all('admin_menu','',array(),$per_page,$this->uri->segment($uri),array("order"=>"asc"));
			}	
																															
			$config['base_url']     =  site_url('back_end/admin_menu/index/');
			$config['total_rows']   =  $this->db->count_all('admin_menu');
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			
			$this->pagination->initialize($config);
			$this->load->view('back_end/template_backend',$data);	
		}
		
		function order() {
			//lay ra cac danh muc
			$this->db->order_by('id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$order_admin_menu = $this->input->post('order_admin_menu');
			
			//dem so danh muc 
			$count_order = count($data['category_admin']);
			$i = 0;
				
			foreach ($order_admin_menu as $order_admin_menu_key => $order_admin_menu_value) {
				//admin menu can thay doi order 
				$this->db->select('id,order');
				$data['order_adm'] = $this->db->get_where('admin_menu',array('id' => $order_admin_menu_key))->row_array();
				
				//admin_menu trung voi gia tri can thay doi
				$this->db->select('id,order');
				$data['order_adm_value'] = $this->db->get_where('admin_menu',array('order' => $order_admin_menu_value))->row_array();
					
				//hoa doi gia tri order
				if (!empty($data['order_adm_value'])) {
					if ($data['order_adm']['id'] !== $data['order_adm_value']['id']) {
						$where_1 = array(
			  				'id' => $data['order_adm']['id']
			  			);
			  			
			  			$order_1 = array(
			  				'id' => 'DESC' 
			  			);
			  	  
						$data_value_1 = array (
							'order' => $data['order_adm_value']['order'],
						);	
						
						$this->mod_lib->update('admin_menu',$where_1,$order_1,$data_value_1);
						
						$where_2 = array(
			  				'id' => $data['order_adm_value']['id']
			  			);
			  			
			  			$order_2 = array(
			  				'id' => 'DESC' 
			  			);
			  	  		
						$data_value_2 = array (
							'order' => $data['order_adm']['order'],
						);	
						
						$this->mod_lib->update('admin_menu',$where_2,$order_2,$data_value_2);
						
						$this->session->set_flashdata('msview', 'Thay đổi thứ tự thành công !');
						redirect('back_end/admin_menu');
					} 		
				}	
			}
			
			redirect('back_end/admin_menu');
		}	
		
		function search () {
			$data['vl_sort'] = "DESC";	
			$check_search = $this->input->POST('formSearch');
			$per_page = 10;
			$data['title'] = "Admin Menu - Search";
			$data['table'] = 'admin_menu';
			$data['controller'] = 'admin_menu';
			$data['list_menu'] = $this->mod_lib->findOrderList($data['table']);
			$data['content_admin'] = 'back_end/admin_menu/index';
			$data['sort'] = 'ASC';
			$this->db->order_by('parent_id','asc');

			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			if(!empty($check_search)){
				$_SESSION['like_admin_menu'] = array('name' =>$this->input->POST('nameSearch'));																												
			} else {
				$_SESSION['like_admin_menu'] = array();
			}
			
			$data_search = $this->mod_lib->search('admin_menu','',$_SESSION['like_admin_menu'],array(),array("id"=>"desc"));
			$data['admin_menu_value'] = $this->mod_lib->load_search('admin_menu','',$_SESSION['like_admin_menu'],array(),$per_page,$this->uri->segment(4),array("id"=>"desc"));		
			$config['base_url']     =  site_url('back_end/admin_menu/search/');
			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  4; 
			$this->pagination->initialize($config);		
			$this->load->view('back_end/template_backend',$data);	
		}
		
		function sort($field,$sort = '') {
			$per_page = 10;
			$uri = 4;
			$data['title'] = "Admin Menu - Sort";
			$data['table'] = 'admin_menu';
			$data['controller'] = 'admin_menu';
			$data['list_menu'] = $this->mod_lib->findOrderList($data['table']);
			$data['content_admin'] = 'back_end/admin_menu/index';
			$this->db->order_by('parent_id','asc');
	
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			$uri_value_4 =  $this->uri->segment(4);
			$uri_value_5 =  $this->uri->segment(5);
			
			$_SESSION['order_sort'] = array(
				$uri_value_4 => $uri_value_5 
			);
			
			$_SESSION['sort'] = $sort;
			$_SESSION['field'] = $field;
			
			if ($sort == 'DESC') {
				$data['sort'] =  'ASC';
			} else if($sort == 'ASC') {
				$data['sort'] =  'DESC';
			}
			
			$data_search = $this->mod_lib->search('admin_menu','',array(),array(),array("id"=>"desc"));
			$data['admin_menu_value'] = $this->mod_lib->load_search('admin_menu','',array(),array(),$per_page,$this->uri->segment($uri),array($_SESSION['field']=>$_SESSION['sort']));		
			$config['base_url']     =  site_url('back_end/admin_menu/index');

			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);		
			$this->load->view('back_end/template_backend',$data);
		}
		
		function add(){
			$data['title']			= "Thêm danh mục quản lý - admin";
			$data['content_admin'] = 'back_end/admin_menu/add';
			//lay ra danh muc menu theo thu tu sap xep
			$data['list_menu'] = $this->mod_lib->findOrderList('admin_menu');
			$data['table'] = 'admin_menu';
			$add = $this->input->post('addform');
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			//lay ra so order 
			$controller_admin_menu = $this->db->get('admin_menu')->result_array();
					
			if(!empty($add)){ 	
				$data['admin_menu_value'] = $this->db->get('admin_menu')->result_array();
				$data['content_admin'] = 'back_end/admin_menu/index';
				
				if( $this->input->post('parent_id') == 0) {
					$arr_pr  = array();
				} else {
					$this->db->select('parent_id');
					$arr_menu = $this->db->get_where('admin_menu', array('id' => $this->input->post('parent_id')))->row_array();
					$arr_pr  = explode(".",$arr_menu['parent_id']);
				}
				
				if(count($arr_pr) == 0) {
					$level = 1;
				} else if (count($arr_pr) == 1){
					$level = 2;  	
				}
				
				if(trim($this->input->post('link')) == ''){
					$link = '#';
				} else {
					$link = trim($this->input->post('link'));
				}
				
				if(trim($this->input->post('controller')) == ''){
					$controller = '#';
				} else {
					$controller = trim($this->input->post('controller'));
				}
				
				$data_value = array (
							'name'       => trim($this->input->post('name')),
							'controller' => $controller,
							'order'      => count($controller_admin_menu) + 1,
							'link'       => $link,
							'status'     => trim($this->input->post('status')),
							'level'      => $level				
						);
				
				if($this->mod_lib->insert('admin_menu',$data_value)) {
					$id = $this->db->insert_id();
					
				  	$where = array(
				  			'id' => $id 
				  		);
				  	$order = array (	
				  			'id' => 'desc'
				  		);	
				  		
				  	if ($this->input->post('parent_id') > 0) {
						$data  = array(
							'parent_id'  => $arr_menu['parent_id'].'.'.$id,
							'root_path' => $this->input->post('parent_id')	
						);
				  	} else {
				  		$data  = array(
				  			'parent_id' => $id,
				  			'root_path' => $this->input->post('parent_id')	
				  		);
				  	}
					
					$this->mod_lib->update('admin_menu',$where,$order,$data);
					
					$Arr_admins = $this->db->get('admins')->result_array();
					foreach ($Arr_admins as $admin) {
						$Ad_mn = $this->db->get_where('admin_menu',array('id' => $id))->row_array();
						
						if ($Ad_mn['link'] !== '#') {
							$data_permiss  = array(
								'admin_menu_id' => $id,
								'admins_id'     => $admin['id']
							);
							
							$this->mod_lib->insert('permiss_admins',$data_permiss);
						}	
					}
					
					$this->session->set_flashdata('msview', 'Thêm thành công !');
					$this->mod_lib->insert_log('Add admin_menu',$_SESSION['admin_id']);
				} else {	
					$this->session->set_flashdata('msview', 'Thêm thất bại !');
					$this->load->view('back_end/template_backend',$data);
				}
							
				redirect('back_end/admin_menu');			
			} else {
				
				$this->load->view('back_end/template_backend',$data);
			}
		}
		
		function edit($id=null){
			$data['title']			= "Sửa admin menu";
			$data['content_admin'] = 'back_end/admin_menu/edit';
			$data['list_menu'] = $this->mod_lib->findOrderList('admin_menu');
			$data['admin_menu_value'] = $this->db->get_where('admin_menu',array( 'id' => $id ))->row_array();
			$edit = $this->input->post('editForm');
			$this->db->order_by('parent_id','asc');
			
			//lay ra danh muc menu theo thu tu sap xep
			$data['list_menu'] = $this->mod_lib->findOrderList('admin_menu');
			$data['table'] = 'admin_menu';
			$add = $this->input->post('addform');
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
					
			if ($id == $this->input->post('parent_id')) {
				$this->session->set_flashdata('msview', 'Sửa thất bại !');
				redirect('back_end/admin_menu');
			}
			
			if( $this->input->post('parent_id') == 0) {
				$arr_pr  = array();
			} else {
				$this->db->select('parent_id');
				$arr_menu = $this->db->get_where('admin_menu', array('id' => $this->input->post('parent_id')))->row_array();
				$arr_pr  = explode(".",$arr_menu['parent_id']);
			}
			
			if(count($arr_pr) == 0) {
				$level = 1;
			} if (count($arr_pr) == 1){
				$level = 2;  	
			} else if (count($arr_pr) == 2) {
				$level = 3;
			}
			
			if(trim($this->input->post('link')) == ''){
				$link = '#';
			} else {
				$link = trim($this->input->post('link'));
			}
			
			if(trim($this->input->post('controller')) == ''){
				$controller = '#';
			} else {
				$controller = trim($this->input->post('controller'));
			}
			
			if(!empty($edit)){					
				$where = array(
			  			'id' => $id 
			  		);
			  	$order = array(
			  			'id' => 'DESC' 
			  		);
			  		
			  	if ($this->input->post('parent_id') > 0) {
					$data_value = array (
						'name'       => trim($this->input->post('name')),
						'controller' => trim($this->input->post('controller')),
						'link'       => $link,
						'status'     => trim($this->input->post('status')),
						'level'      => $level,	
						'parent_id'  => $arr_menu['parent_id'].'.'.$id,
						'root_path' => $this->input->post('parent_id')
					);
			  	} else {
			  		$data_value = array (
						'name'       => trim($this->input->post('name')),
						'controller' => trim($this->input->post('controller')),
						'link'       => $link,
						'status'     => trim($this->input->post('status')),
						'level'      => $level,	
						'parent_id'  =>  $id,
			  			'root_path' => '0'
					);
			  	}
					
				if($this->mod_lib->update('admin_menu',$where,$order,$data_value)){
					$this->session->set_flashdata('msview', 'Sửa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Sửa thất bại !');
				}
				
				redirect('back_end/admin_menu');
			}else {		
				$this->load->view('back_end/template_backend',$data);		
			}
		} 
		
		function delete($id = null) {
			//lay ra order cua ban ghi bi xoa
			$this->db->select('order');
			$admin_menu_one = $this->db->get_where('admin_menu',array('id' => $id))->row_array();
			$order_one = $admin_menu_one['order'];
			
			//xoa admin menu
			$this->db->delete('admin_menu', array('id' => $id));
			
			//lay ra cac ban ghi co gia tri order lon hon gia tri order cua ban ghi da xoa
			$this->db->select('id,order');
			$this->db->where("order > $order_one");
			$this->db->order_by("order", "asc");
			$admin_menu_arr = $this->db->get('admin_menu')->result_array();
			
			//cong them moi gia order 1 don vi
			foreach ($admin_menu_arr as $admin_menu_arr_value) {
				$where = array(
			  			'id' => $admin_menu_arr_value['id']
			  		);
			  		
			  	$order = array();
			  	
				$data_value = array (
					'order'       => $admin_menu_arr_value['order'] - 1
				);
				
				$this->mod_lib->update('admin_menu',$where,$order,$data_value);
			}
			
			//xoa quyen tuong ung cua nguoi dung 
			$this->db->delete('permiss_admins', array('admin_menu_id' => $id));
			
			$this->session->set_flashdata('msview', 'Xóa thành công !');
			redirect('back_end/admin_menu');
		}
		
		function deleteAll() {
			$value_id_arr    = $this->input->post('value_id');
			
			foreach ($value_id_arr as $value_id_arr_key => $value_id_arr_value) {
				//lay ra order cua ban ghi bi xoa
				$this->db->select('order');
				$admin_menu_one = $this->db->get_where('admin_menu',array('id' => $id))->row_array();
				$order_one = $admin_menu_one['order'];
				
				//xoa admin menu
				$this->db->delete('admin_menu', array('id' => $id));
				
				//lay ra cac ban ghi co gia tri order lon hon gia tri order cua ban ghi da xoa
				$this->db->select('id,order');
				$this->db->where("order > $order_one");
				$this->db->order_by("order", "asc");
				$admin_menu_arr = $this->db->get('admin_menu')->result_array();
				
				//cong them moi gia order 1 don vi
				foreach ($admin_menu_arr as $admin_menu_arr_value) {
					$where = array(
				  			'id' => $admin_menu_arr_value['id']
				  		);
				  		
				  	$order = array();
				  	
					$data_value = array (
						'order'       => $admin_menu_arr_value['order'] - 1
					);
					
					$this->mod_lib->update('admin_menu',$where,$order,$data_value);
				}
				
				//xoa quyen tuong ung cua nguoi dung 
				$this->db->delete('permiss_admins', array('admin_menu_id' => $id));
			}
			
			$this->session->set_flashdata('msview', 'Xóa thành công !');	
			redirect('back_end/admin_menu');
		}
		
	}
?>